﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CapaDeNegocio;
using CapaDeDominio;

namespace Studio7iMVC2.Controllers
{
    public class ClienteController : Controller
    {
        //
        // GET: /Cliente/
        //region Dependencias a inyectarse con Spring.NET
        #region
        IAdministracionService AdminService = null;
        #endregion


        public ActionResult Index()
     
            {
                ICollection<Cliente> modelo = AdminService.ListarClientes();
                return View ( modelo );
            }
            
      

        //
        // GET: /Cliente/Details/5

        public ActionResult Detalles(string Codigo)
        {
            Cliente modelo = AdminService.ObtenerCliente(Convert.ToInt32(Codigo));
            return View (modelo);
        }

        //
        // GET: /Cliente/Create

        public ActionResult Create()
        {
           return View ();
        } 

        //
        // POST: /Cliente/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                if     (ModelState.IsValid)
                {
                  

                   // Cliente clienteAcrear = new Cliente ( );
                    //clienteAcrear.Codigo = int.Parse(collection [ "Codigo" ]);
                    string Nombres = (string)collection["Nombres"];
                    string Apellidos = (string)collection["Apellidos"];
                    Maestra Tip_Documento = AdminService.ObtenerTipoDoc(Convert.ToInt32(collection["tip_documento"]));
                    string Nro_Documento = (string)collection["Nro_Documento"];
                    string Correo = (string)collection["Correo"];
                    DateTime Fec_Nacimiento = DateTime.Parse(collection["Fec_Nacimiento"]);
                    string Estado = (string)collection["Estado"];
                    //Usuario Usuario = AdminService.ObtenerUsuario(Convert . ToInt32( collection [ "Usuario" ]));

                    Cliente ClienteCreado = AdminService.RegistrarCliente(Nombres, Apellidos, Convert.ToInt32(collection["tip_documento"]), Nro_Documento, Correo, Fec_Nacimiento, "", "", Estado);

                   TempData [ "mensaje" ] = "Cliente " + ClienteCreado . Codigo + " Registrado satisfactoriamente";
                }

                return RedirectToAction("Index");
            }
            catch
            {
                ModelState . AddModelError ( string . Empty , "Error Al crear cliente" );
                return View();
            }
        }
        
        //
        // GET: /Cliente/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Cliente/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Cliente/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Cliente/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
